﻿using BaoVeThanhDia.GamePlay.Towers;
using BaoVeThanhDia.GamePlay.UpGrade;
using BaoVeThanhDia.MapFolder;
using BaoVeThanhDia.Sound;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;

namespace BaoVeThanhDia.GamePlay
{
    class UpgradeTower
    {
        int Level, Type;
        float Range;
        Vector2 Index, position;
        Texture2D ImageTam;
        List<UpgradeTowerButton> buttons;
        int chiso;
        bool isVisible;
        CustomSoundEffect sound;
        public UpgradeTower()
        {
            isVisible = false;
        }
        public UpgradeTower(Tower tower, int chiso)
        {
            sound = new CustomSoundEffect("sounds/effect/Tower/Upgrade_Tower");
            isVisible = true;
            this.chiso = chiso;
            buttons = new List<UpgradeTowerButton>();
            Level = tower.getLevel();
            Type = tower.getType();
            Index = tower.getIndex();
            Range = tower.getRange() * 128;
            position = new Vector2(Index.Y, Index.X) * 128;
            buttons.Add(new UpgradeTowerButton(tower, 0));
            buttons.Add(new UpgradeTowerButton(tower, 1));
            buttons.Add(new UpgradeTowerButton(tower, 2));
            buttons.Add(new UpgradeTowerButton(tower, 3));
        }
        public void Load(ContentManager cont)
        {
            sound.Load(cont);
            ImageTam = cont.Load<Texture2D>("images/upgradeTower/aoe-circle");
            foreach (UpgradeTowerButton button in buttons)
            {
                button.Load(cont);
            }
        }
        public void Draw(SpriteBatch sprite, SpriteFont font, ContentManager cont)
        {
            sprite.Draw(ImageTam, (position + ((512 - Range) * new Vector2(0.15f, 0.15f)) - new Vector2((float)Math.Sqrt(2) - 0.5f, (float)Math.Sqrt(2) - 0.5f) * Range) * GamePage.Scale + GamePage.Dolech, new Rectangle(0, 0, 144, 144),
                                                 Color.White, 0, Vector2.Zero, (Range * 2 / 144) * GamePage.Scale, SpriteEffects.None, 0);
            foreach (UpgradeTowerButton button in buttons)
            {
                button.Draw(sprite, font, cont);
            }
        }
        public void isTouch(Vector2 touch, ContentManager cont, bool isDoThis = false)
        {
            Vector2 temp = GamePage.getIndexTouch(touch);
            if (Vector2.Distance(temp, Index) >= 2)
            {
                isVisible = false;
            }
            else
            {
                foreach (UpgradeTowerButton button in buttons)
                {
                    if (button.isTouched(touch) && isDoThis)
                    {
                        sound.Play();
                        int tempType = button.getTypeButton();
                        if (tempType == 3)
                        {
                            GamePage.Money += button.getPrice();

                            Tower tower = GamePage.towers[chiso];
                            if (tower.GetType() == typeof(MagicTower))
                            {
                                GamePage.countMagicTower++;
                            }

                            GamePage.towers.RemoveAt(chiso);

                            Map.matrix[(int)Index.X, (int)Index.Y] = 1;
                            isVisible = false;
                        }
                        else
                        {
                            Tower tower = GamePage.towers[chiso];
                            tower.setLevel(Level + 1, cont, tempType == 2 ? 0 : tempType + 1);
                            tower.setLevel(Level + 1, tempType == 2 ? 0 : tempType + 1);
                            tower.setelement();
                            if (GamePage.Money >= (int)tower.getPrice())
                            {
                                GamePage.Money -= (int)tower.getPrice();
                                GamePage.towers.Add(tower);
                                GamePage.towers.RemoveAt(chiso);
                            }
                            isVisible = false;
                        }
                    }
                }
            }
        }
        public bool getVisible() { return isVisible; }
        public void setVisible(bool t) { isVisible = t; }
    }
}
